package com.wuye.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wuye.dao.TousuDaoImpl;
import com.wuye.dao.UsersDaoImpl;
import com.wuye.dao.WeixiuDaoImpl;
import com.wuye.entity.Tousu;
import com.wuye.entity.Weixiu;

@Service("weixiuService")
public class WeixiuService {
	@Resource
	private WeixiuDaoImpl weixiuDao;
	@Resource
	private UsersDaoImpl userDao;

	public void save(Weixiu weixiu) {
		weixiu.setType(0);
		weixiu.setShijian(new Date());
		weixiuDao.save(weixiu);
	}

	public void del(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Weixiu weixiu = new Weixiu();
			weixiu.setId(Integer.parseInt(sz[i]));
			weixiuDao.del(weixiu);
		}
	}

	public void update(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Weixiu	weixiu = weixiuDao.getObject(Integer.parseInt(sz[i]));
			weixiu.setShijian(new Date());
			weixiu.setType(1);
			weixiuDao.update(weixiu);
		}
	}

	public Map getList(int offset, int length) {
		String[] title = { "id", "neirong", "shijian", "username" };
		return weixiuDao
				.getlist(
						title,
						"select w.id,w.neirong,substring(w.shijian,1,10),w.users.name from Weixiu w where w.type=0",
						offset, length);
	}

	public Map getSearchList(Weixiu weixiu, int offset, int length,String shijian1,String shijian2)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		String[] title = { "id", "neirong", "shijian", "username" };
		sb.append("select w.id,w.neirong,substring(w.shijian,1,10),w.users.name from Weixiu w ");
		if (weixiu.getType() != 2) {
			sb.append("where w.type=" + weixiu.getType());
			if(!shijian1.equals("")){
				sb.append(" and w.shijian>='"+shijian1+"'");
			}
			if(!shijian2.equals("")){
				sb.append(" and w.shijian<='"+shijian2+"'");
			}
		}else{
			if(!shijian1.equals("")){
				sb.append(" where w.shijian>='"+shijian1+"'");
				if(!shijian2.equals("")){
					sb.append(" and w.shijian<='"+shijian2+"'");
				}
			}else{
				if(!shijian2.equals("")){
					sb.append(" where w.shijian<='"+shijian2+"'");
				}
			} 
		}
		return weixiuDao.getlist(title, sb.toString(), offset, length);
	}

	public UsersDaoImpl getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDaoImpl userDao) {
		this.userDao = userDao;
	}

	public WeixiuDaoImpl getWeixiuDao() {
		return weixiuDao;
	}

	public void setWeixiuDao(WeixiuDaoImpl weixiuDao) {
		this.weixiuDao = weixiuDao;
	}
}
